package cn.sun.org.service.impl;

import cn.sun.basic.service.impl.BaseServiceImpl;
import cn.sun.org.domain.Employee;
import cn.sun.org.domain.Shop;
import cn.sun.org.mapper.EmployeeMapper;
import cn.sun.org.mapper.ShopMapper;
import cn.sun.org.query.ShopQuery;
import cn.sun.org.service.IEmployeeService;
import cn.sun.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop, ShopQuery> implements IShopService {
    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private IEmployeeService employeeService;

    @Override
    public void batchdel(List<Long> ids) {
        shopMapper.batchdel(ids);
    }

    @Override
    public void settledIn(Shop shop) {
        //对用户进行校验
        // 1 判断是否存在，提示“已经入驻，请直接登陆！如果忘记了请找回密码！”
        Employee admin = shop.getAdmin();
        Employee employee = employeeMapper.findByUser(admin);
        if (employee!=null)
            //不仅要错误，还要提示信息
            throw new RuntimeException("已经入驻，请直接登陆！如果忘记了请找回密码！");
        // 2 如果不存在，走入驻流程
        // 2.1 保存管理员信息并放回自增id
        employeeService.save(admin);
        // 2.2 给shop添加admin.id，再保存Shop----名称可以重复但是店铺地址在方圆几公里不能重复入驻，由审核人员把控。
        shopMapper.save(shop);  //上一步保存完毕shop.admin里面就有id


        //让管理员关联店铺id
        admin.setShop(shop);
        employeeMapper.update(admin);
    }
}
